package com.wk.springboot;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
import net.sf.jsqlparser.statement.select.SubSelect;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.util.deparser.SelectDeParser;

import java.util.ArrayList;
import java.util.List;

public class JSqlParserExample {
    public static void main(String[] args) {
        try {
            // 构建 SQL 函数
            Function function = new Function();
            function.setName("CONCAT");
            List<Expression> expressions = new ArrayList<>();
            expressions.add(new StringValue("'Hello, '"));
            expressions.add(new StringValue("'World!'"));
            function.setParameters(new ExpressionList(expressions));

            // 创建 SELECT 语句
            Select select = new Select();
            SelectBody selectBody = new PlainSelect();

            // 创建 SELECT item
            SelectItem selectItem = new SelectExpressionItem(function);
            List<SelectItem> selectItems = new ArrayList<>();
            selectItems.add(selectItem);

            ((PlainSelect) selectBody).setSelectItems(selectItems);
            select.setSelectBody(selectBody);

            // 输出构建的 SQL 语句
            System.out.println(select);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
